package com.zhangdi.mgsync.core.thread;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author zhangdi
 * @version 1.0
 * @date 2021.11.10 16:27
 */
public class MineThreadFactory implements ThreadFactory {
    private String threadNamePrefix;
    private int threadPriority = 5;
    private boolean daemon = false;
    private final AtomicInteger threadCount = new AtomicInteger();

    public String getThreadNamePrefix() {
        return threadNamePrefix;
    }

    public void setThreadNamePrefix(String threadNamePrefix) {
        this.threadNamePrefix = threadNamePrefix;
    }

    public void setThreadPriority(int threadPriority) {
        this.threadPriority = threadPriority;
    }

    public boolean isDaemon() {
        return daemon;
    }

    public void setDaemon(boolean daemon) {
        this.daemon = daemon;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread thread = new Thread(null, r, defaultThreadName(), threadPriority);
        thread.setDaemon(daemon);
        return thread;
    }

    private String defaultThreadName(){
        return threadNamePrefix + threadCount.incrementAndGet();
    }
}
